/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pa165.languageschoolpersistence.DAOImpl;

import com.pa165.languageschoolpersistence.DAO.LevelOfLanguageDAO;
import com.pa165.languageschoolpersistence.Exceptions.IllegalArgumentPersistenceException;
import com.pa165.languageschoolpersistence.Exceptions.ServiceFailureException;
import com.pa165.languageschoolpersistence.entities.Lector;
import com.pa165.languageschoolpersistence.entities.LevelofLanguage;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Elena Medvedeva
 */
@Repository("levelOfLanguageDAO")
public class LevelOfLanguageDAOImpl implements LevelOfLanguageDAO {

    private EntityManager em;

    @PersistenceContext
    public void setEntityManager(EntityManager em) {
        this.em = em;
    }

    public LevelofLanguage add(LevelofLanguage levofLang) throws IllegalArgumentPersistenceException, ServiceFailureException {
        if (levofLang == null) {
            throw new IllegalArgumentPersistenceException("Method LevelOfLanguageDAOImpl.add call with Null argument levofLang");
        }
        if ((levofLang.getLanguageSpoken() == null) || (levofLang.getLector() == null) || (levofLang.getLevel() == null)) {
            throw new IllegalArgumentPersistenceException("Method LevelOfLanguageDAOImpl.add call with Null field of argument levofLang");
        }
        em.persist(levofLang);
        return levofLang;
    }

    public void remove(LevelofLanguage levofLang) throws IllegalArgumentPersistenceException, ServiceFailureException {
        if ((levofLang == null) || (levofLang.getId() == null)) {
            throw new IllegalArgumentPersistenceException("Method LevelOfLanguageDAOImpl.remove call with Null argument levofLang or its id");
        }

        LevelofLanguage levofLangToRemove = em.find(LevelofLanguage.class, levofLang.getId());
        em.remove(levofLangToRemove);
    }

    public LevelofLanguage modify(LevelofLanguage levofLang) throws IllegalArgumentPersistenceException {
        if (levofLang == null || levofLang.getId() == null) {
            throw new IllegalArgumentPersistenceException("Method LevelOfLanguageDAOImpl.modify call with Null argument levofLang or its id");
        }
        if ((levofLang.getLanguageSpoken() == null) || (levofLang.getLector() == null) || (levofLang.getLevel() == null)) {
            throw new IllegalArgumentPersistenceException("Method LevelOfLanguageDAOImpl.modify call with Null field of argument levofLang");
        }
        em.merge(levofLang);
        return levofLang;
    }

    public LevelofLanguage get(Long id) throws IllegalArgumentPersistenceException {
        if (id == null) {
            throw new IllegalArgumentPersistenceException("Method LevelOfLanguageDAOImpl.get call with Null argument id");
        }
        LevelofLanguage levofLang = null;
        levofLang = em.find(LevelofLanguage.class, id);
        return levofLang;
    }

    public List<LevelofLanguage> get(Lector lector) throws IllegalArgumentPersistenceException {
        if ((lector == null) || (lector.getId() == null)) {
            throw new IllegalArgumentPersistenceException("Method LevelOfLanguageDAOImpl.get(lector) call with Null argument lector or its id:");
        }
        List<LevelofLanguage> result = null;
        result = em.createQuery("select ll from LevelofLanguage ll join fetch ll.lector l where l.id=:id ", LevelofLanguage.class).setParameter("id", lector.getId()).getResultList();
        return result;
    }
}
